//参考的题解 滑动窗口 可扩展为取出最长字串
console.log('pwwkewgfhdgrtjrhtsreghejrsevcxhjabcdefghijklmnopqrstuvwxyz'.length)
var lengthOfLongestSubstring = function (s) {
    let n = s.length;
    let hashMap = new Map();
    
    let ans = 0;
    for (let i = 0, j = 0; i < n; i++) {
      if (hashMap.has(s[i])) {
        j = Math.max(hashMap.get(s[i]), j)
      }
     
      ans = Math.max(ans, i - j + 1);
      //console.log(i,j,ans,'埃及')
      hashMap.set(s[i], i + 1)
    }
    let size = hashMap.size
    //console.log(Array.from(hashMap),'kkkkkkkkkkkkkkk')
    return {hashMap,size,ans};
  };
 console.time('01')
 console.log(lengthOfLongestSubstring('6pwwkewgfhdgrtjrhtsreghejrsevcxhjabcdefghijklmnopqrstuvwxyzp').ans,'??????????????????????????????????????????????????????????')
 console.timeEnd('01')

 var lengthOfLongestSubstring2 = function(s) {
  let num=0,j=0,t=0
  for(let i=0;i<s.length;i++){
    t = s.slice(j,i).indexOf(s[i]);
      if(t == -1){
          num=Math.max(num,i-j+1)
      }else{
        //console.log(j,'j')
          j=j+t+1
          //console.log(i,j,num,t,'ssss')
      }
      console.log(i,j,num,t,'ssss')
  }
  return num
};

console.time('02')
console.log(lengthOfLongestSubstring2('6pwwkewgfhdgrtjrhtsreghejrsevcxhjabcdefghijklmnopqrstuvwxyzp'))
console.timeEnd('02')
var lengthOfLongestSubstring3 = function (s) {
  let maxNum = 0;
  let stIndexArr = []; // 当前位置最长字串的起始位置游标
  for (let i = 0; i < s.length; i++) {
      if (i == 0) {
          maxNum = 1;
          stIndexArr[i] = 0;
          continue;
      }
      let nowChar = s[i];
      for (let j = stIndexArr[i - 1]; j < i; j++) {
          if (nowChar == s[j]) {
              stIndexArr[i] = j + 1;
              break;
          }
      }
      if (stIndexArr[i] == undefined) {
          stIndexArr[i] = stIndexArr[i - 1];
      }
      if ((i - stIndexArr[i] + 1) > maxNum) {
          maxNum = i - stIndexArr[i] + 1;
      }
  }
  return maxNum;
};
console.time('03')
console.log(lengthOfLongestSubstring3('pwwkewgfhdgrtjrhtsreghejrsevcxhjabcdefghijklmnopqrstuvwxyz'))
console.timeEnd('03')